package com.dengyuanke.wx.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.dengyuanke.common.result.JsonResult;
import com.dengyuanke.entity.User;
import com.dengyuanke.service.IUserService;
import com.dengyuanke.utils.jwt.JWT;
import com.dengyuanke.wx.service.WeixinService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author dengyuanke
 * @since 2017-11-07
 */
@Api(tags = "用户信息相关API")
@RestController
public class UserController {

    @Autowired
    private WeixinService wxService;

    @Autowired
    private IUserService userService;

    @ApiOperation(value="获得用户信息", notes="请求须带上token字段")
    @PostMapping("/getuserinfo")
    public JsonResult getUserInfo(@RequestAttribute User currentUser) {
      return   JsonResult.renderSuccess(currentUser);
    }

    @ApiOperation(value="获得token", notes="必须传入code字段")
    @PostMapping("/gettoken")
	public JsonResult getToken(@ApiParam(value="微信端传过来的code字段",name="code",required=true)@RequestParam(value = "code",required = true) String code,
                                  @ApiParam(value="微信端传过来的lang字段",name="lang",required=false)@RequestParam(value = "lang",required = false) String lang) throws WxErrorException {
        WxMpOAuth2AccessToken accessToken= this.wxService.oauth2getAccessToken(code);
        WxMpUser wxMpUser = this.wxService.getUserService().userInfo(accessToken.getOpenId(), lang);
        User userInfo = userService.selectOne(new EntityWrapper<User>().eq("open_id", wxMpUser.getOpenId()));
        if(userInfo==null){
            userInfo.setId(IdWorker.get32UUID());
            userInfo.setName(wxMpUser.getNickname());
            userInfo.setHeadUrl(wxMpUser.getHeadImgUrl());
            userInfo.setOpenId(userInfo.getOpenId());
            userService.insert(userInfo);
        }
        Map<String , Object> payload=new HashMap<String, Object>(16);
        payload.put("user", userInfo);
        String token = JWT.createToken(payload);
        return JsonResult.renderSuccess((Object) token);
    }

    @ApiOperation(value="更新用户信息", notes="传入带id的User实体")
    @PostMapping("/updateuserinfo")
    public JsonResult updateUserInfo(User user){
        if(StringUtils.isEmpty(user.getId())){
            return JsonResult.renderFail("传入用户的id为空，更新错误");
        }
        boolean b = userService.updateById(user);
        if (b){
            return JsonResult.renderSuccess("更新成功");
        }else {
            return JsonResult.renderSuccess("更新失败");
        }
    }

}
